package edu.washington.vaccine.ui;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class FirstGroup extends ActivityGroup {
	public static FirstGroup group;
	private ArrayList<View> history;
	
	@SuppressWarnings("static-access")
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.history = new ArrayList<View>();
		this.group = this;
		
    	View view = getLocalActivityManager().startActivity("VaccineActivity", new Intent(this, VaccineActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    	history.add(view);
    	setContentView(view);
	}
	
	public void toast(String message) {
		Toast.makeText(getLocalActivityManager().getCurrentActivity(), message, Toast.LENGTH_SHORT).show();
	}
	
	public void back() {
		if (history.size() > 1) {
			pop();
			setContentView(history.get(history.size() - 1));
		} else {
//			finish(); // app wasn't fully closing with finish()
			android.os.Process.killProcess(android.os.Process.myPid());
		}
	}
	
	public void home() {
		for (int i = history.size() - 1; i > 0; i--) {
			history.remove(i);
		}
		setContentView(history.get(0));
	}
	
	public void addChildActivity(String id, Intent newIntent) {
    	View view = FirstGroup.group.getLocalActivityManager().startActivity(id, newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
    	history.add(view);
    	setContentView(view);
	}
	
	public void onBackPressed() {
		FirstGroup.group.back();
		return;
	}
	
	public void pop() {
		history.remove(history.size() - 1);
	}
}
